package com.qzh;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.qzh.baseStruct.simpleStruct.Book;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.SneakyThrows;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

import static com.qzh.utils.Chinese.numberToChinese;

/**
 * 生产者
 */
@Data
@AllArgsConstructor
public class Producer implements Runnable {
    private int id;
    private BlockingDeque<Book> deque;
   private List<Book> bookList;
   public Producer(BlockingDeque<Book> deque, List<Book> bookList){
       this.deque = deque;
       this.bookList = bookList;
   }
    @SneakyThrows
    @Override
    public void run() {
        int i=1;
        while (i<=1000){
            Book book = new Book().setName(numberToChinese(i)).setISBN(IdUtil.fastSimpleUUID()).setAuthor(RandomUtil.randomString(5));
            deque.put(book);
            bookList.add(book);
            System.out.println("生产者已放入新的书籍，放置后缓冲区的大小为："+deque.size()+",总书籍数量为："+bookList.size());
            Thread.sleep(1500);//放置书本的速度为1秒一本
            i++;
        }
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}
